
/*
 * ResultDialog.java
 *
 * Created on Jul 2, 2009, 7:31:40 AM
 */

package matpred;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.print.PrinterException;
import java.text.MessageFormat;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.JTable.PrintMode;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import utilities.entities.Result;
import utilities.ResultFixture;

/**
 *
 * @author Demola
 */
public class ResultDialog extends javax.swing.JFrame {

    /** Creates new form ResultDialog */
    public ResultDialog(java.awt.Frame parent, boolean modal) {
//        super(parent, "Result Search Feature", modal);
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (Exception ex) {
            System.err.println(ex.toString());
        }
        initComponents();
        Toolkit tk = Toolkit.getDefaultToolkit();
        Dimension dim = tk.getScreenSize();
        Dimension ndim = new Dimension((dim.width-30), 500);
        setSize(ndim);
        dtm = new DefaultTableModel(){

            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }

        };
//        dtm.
    }

    public ResultDialog(List<Result> result){
        this(null, true);
        this.result = result;

        for(int i = 0; i < result.size(); i++){
            dtm.setRowCount(i + 1);
            fixtures = new ResultFixture(result.get(i), dtm, i);
            fixtures.fillData();
        }

        jTable1.setModel(dtm);
//        jTable1.getColumnModel().getColumn(2).setCellRenderer(new ColRenderer(1));
        setTable();

    }

    private void setTable(){

        
        for(int c : fixtures.cList){
            jTable1.getColumnModel().getColumn(c).setCellRenderer(new ColRenderer());
        }

        for(int c : fixtures.hcList){
            jTable1.getColumnModel().getColumn(c).setCellRenderer(new ColHeader());
        }

        jTable1.setRowHeight(50);
        DefaultTableCellRenderer dtcr = new DefaultTableCellRenderer();
        dtcr.setHorizontalAlignment(JLabel.CENTER);
        dtcr.setBackground(new Color(0,0,0));
        dtcr.setForeground(new Color(255,255,255));
//        Font font = new Font(Font.MONOSPACED, Font.BOLD, 43);
//        dtcr.setFont(font);
//        dtcr.setSize(450, 99);
        for(int i = 0; i < jTable1.getColumnCount(); i++){
            jTable1.getColumnModel().getColumn(i).setPreferredWidth(450);
            jTable1.getColumnModel().getColumn(i).setHeaderRenderer(dtcr);
        }

        jTable1.getColumnModel().setColumnMargin(20);
        jTable1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
//        jTable1.set
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jTable1.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        jTable1.setRowHeight(59);
        jTable1.setRowSelectionAllowed(false);
        jTable1.getTableHeader().setResizingAllowed(false);
        jTable1.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(jTable1);

        jButton1.setText("Close");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });

        jButton2.setText("Print Result");
        jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton2MouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 1157, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jButton2)
                        .addGap(32, 32, 32)
                        .addComponent(jButton1)))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButton1, jButton2});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 355, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jButton1))
                .addGap(27, 27, 27))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked
        this.dispose();
    }//GEN-LAST:event_jButton1MouseClicked

    private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton2MouseClicked
        MessageFormat header = new MessageFormat("header");
        MessageFormat footer = new MessageFormat("footer");
        try {
            jTable1.print(PrintMode.NORMAL, header, footer);
        } catch (PrinterException ex) {
            Logger.getLogger(ResultDialog.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jButton2MouseClicked

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                ResultDialog dialog = new ResultDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    ResultFixture fixtures;
    DefaultTableModel dtm;
    private List<Result> result;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables


    class ColHeader implements TableCellRenderer{

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, final boolean isSelected, boolean hasFocus, int row, int column) {

            if(value == null){
                return new JTable();
            }
            DefaultTableModel tm = (DefaultTableModel) value;
            JTable cellTable = new JTable(tm){

                @Override
                public boolean isRowSelected(int row) {
                    return isSelected;
                }
            };
            for(int i = 0; i < cellTable.getColumnCount(); i++){
                jTable1.getColumnModel().getColumn(i).setPreferredWidth(20);
                jTable1.getColumnModel().getColumn(i).setMaxWidth(20);
            }
            cellTable.setBounds(10, 10, 10, 11);
//            cellTable.setRowHeight(45);
            return cellTable;
        }
    }


    class ColRenderer implements TableCellRenderer{
        
        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, final boolean isSelected, boolean hasFocus, int row, int column) {

            if(value == null){
                return new JTable();
            }
            Object [] val = (Object[])value;
            JTable cellTable = new JTable(new DefaultTableModel(0, val.length)){
                @Override
                public boolean isRowSelected(int row) {
                    return isSelected;
                }
            };
            cellTable.setTableHeader(null);
            DefaultTableModel tm = (DefaultTableModel)cellTable.getModel();
            tm.addRow(val);
            return cellTable;
        }
    }

}
